package com.example.day04.not;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadManager {
    private static ThreadManager manager;
    private final ThreadPoolExecutor executor;

    private ThreadManager(){
        executor = new ThreadPoolExecutor(5,//核心线程数量,核心池的大小
                20,//线程池最大线程数
                30,//表示线程没有任务执行时最多保持多久时间会终止
                TimeUnit.SECONDS,//时间单位
                new LinkedBlockingQueue<Runnable>(),//任务队列,用来存储等待执行的任务
                Executors.defaultThreadFactory(),//线程工厂,如何去创建线程的
                new ThreadPoolExecutor.AbortPolicy());
    }
    public static ThreadManager getInstance(){
        if (manager==null){
            synchronized (ThreadManager.class){
                if (manager==null){
                    manager=new ThreadManager();
                }
            }
        }
        return manager;
    }
    public void execute(Runnable runnable){
        if (runnable==null)return;
        executor.execute(runnable);
    }
    public void remove(Runnable runnable){
        if (runnable==null)return;
        executor.remove(runnable);
    }
}